<?php
/**
 * File keep Llib_List class
 *
 * @author Yaroslav Olekh
 */


/**
 * Llib_List class
 *
 */
class Llib_Form
{
    protected  $ERROR;
    protected  $form;
    protected  $node = 'form';
    protected  $request;
    protected  $data;

	function __construct(Zend_Controller_Request_Abstract $request)
	{

		$this->form[$this->node] = array(
	        '__attributes' => array(
							        'error' => 0,
							        'message' => '',
							        'sent' => 0
							        )
        );

        $this->request = $request;
	}

	function is_success()
	{
		if($this->form[$this->node]['__attributes']['error'] == 0 && $this->form[$this->node]['__attributes']['sent'] = 1) return true;
		else return false;
	}

	function SetSuccess($text = 'Данные успешно сохранены.')
	{
		$this->form[$this->node]['__attributes']['message'] = $text;
		$this->form[$this->node]['__attributes']['error'] = 0;
		$this->form[$this->node]['__attributes']['sent'] = 1;
	}

	function SetError($text)
	{
		$this->form[$this->node]['__attributes']['message'] = $text;
		$this->form[$this->node]['__attributes']['error'] = 1;
		$this->form[$this->node]['__attributes']['sent'] = 1;
	}
        
        function GetError()
	{
		return $this->form[$this->node]['__attributes']['error'];
	}

	function AddData($data)
	{
		$this->form[$this->node] = array_merge($this->form[$this->node], $data);
	}

	function GetFrom()
	{
		return $this->form;
	}

	function GetPostedData($unset = array())
	{
      	$this->data = $this->request->getParams();

     	unset($this->data['module']);
   		unset($this->data['controller']);
      	unset($this->data['action']);
      	unset($this->data['save']);
      	unset($this->data['saveexit']);

      	if(isset($this->data['_module']))
      	{
      		$this->data['module'] = $this->data['_module'];
      		unset($this->data['_module']);
      	}

      	if(isset($this->data['_controller']))
      	{
      		$this->data['controller'] = $this->data['_controller'];
      		unset($this->data['_controller']);
      	}

      	if(count($unset) > 0)
      	foreach ($unset as $v)
      		unset($this->data[$v]);

        return $this->data;
	}

	function GetActiveList($a)
	{
		$data = array(
			array('id' => 1, 'name' => 'активный'),
			array('id' => 0, 'name' => 'не активный')
		);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a && !is_null($a)) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetTopList($a)
	{
		$data = array(
			array('id' => 1, 'name' => 'главная'),
			array('id' => 0, 'name' => 'не главная')
		);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a && !is_null($a)) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetGenderList($a)
	{
		$data = array(
			array('id' => 0, 'name' => '- Не выбран -'),
			array('id' => 1, 'name' => 'мужской'),
			array('id' => 2, 'name' => 'женский')
		);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetDateDayList($a)
	{
		$data = array(
			array('id' => 0, 'name' => 'День:')
		);

		for($i=1; $i<=31; $i++) $data[] = array('id' => $i, 'name' => $i);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetDateMonList($a)
	{
		$data = array(
			array('id' => 0, 'name' => 'Месяц:'),
			array('id' => 1, 'name' => 'Январь'),
			array('id' => 2, 'name' => 'Февраль'),
			array('id' => 3, 'name' => 'Март'),
			array('id' => 4, 'name' => 'Апрель'),
			array('id' => 5, 'name' => 'Май'),
			array('id' => 6, 'name' => 'Июнь'),
			array('id' => 7, 'name' => 'Июль'),
			array('id' => 8, 'name' => 'Август'),
			array('id' => 9, 'name' => 'Сентябрь'),
			array('id' => 10, 'name' => 'Октябрь'),
			array('id' => 11, 'name' => 'Ноябрь'),
			array('id' => 12, 'name' => 'Декабрь')
		);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetDateYearList($a, $present = true, $to = null)
	{
		$data = array(
			//array('id' => 0, 'name' => 'Год:')
		);

		$curr = getdate(time());

		for($i=$curr['year']-($present ? 0 : 15); $i>=($to > 0 ? $to : $curr['year']-80); $i--) $data[] = array('id' => $i, 'name' => $i);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetDateHourList($a)
	{
		$data = array();

		for($i=0; $i<=23; $i++) $data[] = array('id' => $i, 'name' => $i);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

	function GetDateMinList($a)
	{
		$data = array();

		for($i=0; $i<=59; $i++) $data[] = array('id' => $i, 'name' => $i);

    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
	}

}
